package endcourseproject;


public class Utils {

	static final String HEXES = "0123456789ABCDEF";
	static final int IntegerLenInBytes = Integer.SIZE / Byte.SIZE;   
    
	public static int CopyBytesToInt(byte [] bytes,int offset)
	{
		int data = 0;
		
		for (int i = 0; i < IntegerLenInBytes; i++  )
		{
			data += (int) (bytes[offset+i] & 0xFF)<<(8*i); 
		}
		
		return data;
	}

	public static void CopyIntToBytes(byte [] bytes,int offset, int data)
	{
		for (int i = 0; i < IntegerLenInBytes; i++  )
		{
			bytes[offset+i] = (byte) (((data & (0xFF<<(8*i)))>>(8*i)) & 0xFF) ;
		}
	}
	
	public static String getHex( byte [] raw ){
		if ( raw == null ) {
			return null;
		}
		final StringBuilder hex = new StringBuilder( 2 * raw.length );
		for ( final byte b : raw ) {
			hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
		}
		return hex.toString();
	}

	public static String getHex( byte [] raw, int len ){
		if ( raw == null ) {
			return null;
		}
		final StringBuilder hex = new StringBuilder( 2 * len );
		for(int i = 0; i < len;i++)
		{
			hex.append(HEXES.charAt((raw[i] & 0xF0) >> 4)).append(HEXES.charAt((raw[i] & 0x0F)));
		}
		return hex.toString();
	}

	public static String getHex( byte raw ){
		final StringBuilder hex = new StringBuilder( 2);
			hex.append(HEXES.charAt((raw & 0xF0) >> 4)).append(HEXES.charAt((raw & 0x0F)));
		return hex.toString();
	}

	public static boolean Equals(byte[] a, byte[] b, int length)
	{
		for( int i = 0 ; i < length ; i++)
		{
			if( a[i] != b[i] )
			{
				return false;
			}
			
		}
		
		return true;
	}

	public static boolean Equals(byte[] a, byte[] b)
	{
		if (a.length != b.length)
		{
			return false;
		}

		for( int i = 0 ; i < a.length ; i++)
		{
			if( a[i] != b[i] )
			{
				return false;
			}
			
		}
		
		return true;
	}
	
	public static byte[] reverseBytes(byte[] in){
		byte[] out = new byte[in.length];
		for(int i=0;i<in.length;i++){
			out[i]=in[in.length -(1+i)];			
		}
		return out;
	}
	
	
}
